<?php
$lang = $_GET['lang'] ?? 'en';

$lang_texts = [
    'en' => [
        'db_connection_test' => 'Database Connection Test',
        'db_connect_attempt' => 'Attempting to connect to database: ',
        'on' => ' on ',
        'checking_tables' => 'Checking required tables:',
        'table_exists' => 'Table exists',
        'table_missing' => 'Table does not exist',
        'testing_db_ops' => 'Testing database operations:',
        'insert_test_prompt' => 'Successfully inserted test prompt',
        'retrieve_test_prompt' => 'Successfully retrieved test prompt',
        'retrieve_prompt' => 'Retrieved prompt:',
        'test_data_cleaned' => 'Test data cleaned up',
        'db_test_success' => 'Database connection test completed successfully!',
        'db_connection_failed' => 'Database connection failed:',
        'db_test_completed' => 'Database connection test completed'
    ],
    'zh' => [
        'db_connection_test' => '数据库连接测试',
        'db_connect_attempt' => '尝试连接到数据库: ',
        'on' => ' 在 ',
        'checking_tables' => '检查必需的表:',
        'table_exists' => '表存在',
        'table_missing' => '表不存在',
        'testing_db_ops' => '测试数据库操作:',
        'insert_test_prompt' => '成功插入测试提示',
        'retrieve_test_prompt' => '成功检索测试提示',
        'retrieve_prompt' => '检索到的提示:',
        'test_data_cleaned' => '测试数据已清理',
        'db_test_success' => '数据库连接测试成功完成!',
        'db_connection_failed' => '数据库连接失败:',
        'db_test_completed' => '数据库连接测试完成'
    ]
];

$current_text = $lang_texts[$lang];

$install_file = 'config/installed.txt';
if (!file_exists($install_file)) {
    die($current_text['db_test_completed'] . ' Installation not completed. Please run install.php first.');
}

require_once 'config/config.php';

try {
    echo "<h2>" . $current_text['db_connection_test'] . "</h2>\n";
    echo "<p>" . $current_text['db_connect_attempt'] . "$db_name" . $current_text['on'] . "$db_host</p>\n";
    
    $tables = ['prompts', 'chat_history', 'settings'];
    echo "<h3>" . $current_text['checking_tables'] . ":</h3>\n";
    echo "<ul>\n";
    
    foreach ($tables as $table) {
        $stmt = $pdo->query("SHOW TABLES LIKE '$table'");
        if ($stmt->rowCount() > 0) {
            echo "<li style='color: green;'>✓ " . htmlspecialchars($table) . " - " . $current_text['table_exists'] . "</li>\n";
        } else {
            echo "<li style='color: red;'>✗ " . htmlspecialchars($table) . " - " . $current_text['table_missing'] . "</li>\n";
        }
    }
    echo "</ul>\n";
    
    echo "<h3>" . $current_text['testing_db_ops'] . ":</h3>\n";
    
    $test_prompt = "This is a test prompt inserted at " . date('Y-m-d H:i:s');
    $stmt = $pdo->prepare("INSERT INTO prompts (prompt_text) VALUES (?)");
    $stmt->execute([$test_prompt]);
    echo "<p style='color: green;'>" . $current_text['insert_test_prompt'] . "</p>\n";
    
    $stmt = $pdo->query("SELECT prompt_text FROM prompts WHERE prompt_text LIKE 'This is a test prompt%' ORDER BY id DESC LIMIT 1");
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($result) {
        echo "<p style='color: green;'>" . $current_text['retrieve_test_prompt'] . "</p>\n";
        echo "<p><strong>" . $current_text['retrieve_prompt'] . "</strong> " . htmlspecialchars($result['prompt_text']) . "</p>\n";
    } else {
        echo "<p style='color: red;'>✗ Failed to retrieve test prompt</p>\n";
    }
    
    $pdo->prepare("DELETE FROM prompts WHERE prompt_text LIKE 'This is a test prompt%'")->execute();
    echo "<p style='color: blue;'>" . $current_text['test_data_cleaned'] . "</p>\n";
    
    echo "<h3>" . $current_text['db_test_success'] . "</h3>\n";
    
} catch (PDOException $e) {
    echo "<h3 style='color: red;'>" . $current_text['db_connection_failed'] . "</h3>\n";
    echo "<p>" . htmlspecialchars($e->getMessage()) . "</p>\n";
}
?>
